package com.skiving.cloud.admin.controller;

import com.mybatisflex.core.paginate.Page;
import com.skiving.cloud.admin.entity.SysLog;
import com.skiving.cloud.admin.service.SysLogService;
import com.skiving.cloud.common.utils.PageParameter;
import com.skiving.cloud.common.utils.ReturnT;
import com.skiving.cloud.common.utils.UuidUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.*;
import org.noear.solon.core.util.MimeType;

import java.util.Map;

@Slf4j
@Api("日志管理接口")
@Controller
@Mapping("/sysLog")
@Produces(MimeType.APPLICATION_JSON_UTF8_VALUE)
public class SysLogController {
    @Inject
    SysLogService sysLogService;

    @ApiOperation(value = "分页查询日志")
    @Mapping("/queryByPage")
    @Post
    @Produces(MimeType.APPLICATION_JSON_UTF8_VALUE)
    public ReturnT<Page<SysLog>> queryByPage(@Body PageParameter<Map<String, Object>> parameter){
        Page<SysLog> page = sysLogService.queryByPage(parameter);
        return ReturnT.ok(page);
    }

    /**
     * 添加。
     *
     * @param sysLog 日志对象
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @Post
    @Mapping("/save")
    @ApiOperation(value="新增日志")
    @Produces(MimeType.APPLICATION_JSON_UTF8_VALUE)
    public boolean save(@Body SysLog sysLog){
        try{
            if (sysLog.getId() == null) {
                sysLog.setId(UuidUtil.get32UUID());
            }
            return sysLogService.save(sysLog);
        }catch(Exception e){
            log.error(e.getMessage(), e);
            return false;
        }
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @Get
    @Mapping("/getInfo/{id}")
    @ApiOperation(value="根据id获取日志信息")
    public SysLog getInfo(@Path(value = "id") String id){
        return sysLogService.getById(id);
    }

}
